﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;

namespace PdfOperator
{
    public partial class FrmWordToPdf : Form
    {
        public FrmWordToPdf()
        {
            InitializeComponent();
        }

        private void btnSelectFiles_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFiles = new OpenFileDialog();
            openFiles.Multiselect = true;
            openFiles.Filter = "word文档|*.docx;*.doc";
            if (openFiles.ShowDialog() == DialogResult.OK)
            {
                string[] filenames = openFiles.FileNames;
                Array.Sort(filenames);
                foreach (string filename in filenames)
                {
                    lstBoxFileList.Items.Add(filename);
                }
            }
        }

        private void btnSavePath_Click(object sender, EventArgs e)
        {
            if (lstBoxFileList.Items.Count < 1)
            {
                MessageBox.Show("请添加要转换的word文档！");
                return;
            }
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = folderBrowser.SelectedPath;
            }
        }

        /// <summary>
        /// 开始word转PDF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (lstBoxFileList.Items.Count < 1)
            {
                MessageBox.Show("请添加要操作的word文档！");
                return;
            }
            //if (string.IsNullOrEmpty(txtSavePath.Text))
            //{
            //    MessageBox.Show("请选择转换后的PDF文件保存路径！");
            //    return;
            //}

            string args = "";
            for (int i = 0; i < lstBoxFileList.Items.Count; i++)
            {
                string wordfileName = lstBoxFileList.Items[i].ToString();
                // string pdfFileName = (txtSavePath.Text + wordfileName.Substring(wordfileName.LastIndexOf("\\"), wordfileName.LastIndexOf(".") - wordfileName.LastIndexOf("\\")) + ".pdf");

                args = args + wordfileName + " ";

            }
            args = args.Trim();
            //Console.WriteLine(args);
            if (File.Exists("WordToPdf.exe"))   //文件存在
            {
                Console.WriteLine("文件存在");
                try
                {
                    Process myProcess = new Process();
                    // 调用当前目录下的WordToPdf.exe（Python程序）进行转换
                    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("WordToPdf.exe", args);
                    myProcessStartInfo.CreateNoWindow = true;
                    myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    myProcess.StartInfo = myProcessStartInfo;
                    myProcess.Start();

                    while (!myProcess.HasExited)
                    {
                        myProcess.WaitForExit();
                    }

                    _ = myProcess.ExitCode;
                    myProcess.Dispose();
                    myProcess.Close();

                    MessageBox.Show("完成");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
                               
        }

        private void btnOpenSaveFolder_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!System.IO.Directory.Exists(txtSavePath.Text))
            {
                return;
            }
            else
            {
                //存在文件夹
                System.Diagnostics.Process.Start("explorer.exe", txtSavePath.Text);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        
    }
}
